package com.xk.cart.service;

import com.hy.common.utils.JsonUtils;
import com.xk.auth.entiy.UserInfo;
import com.xk.cart.interceptors.CartInterceptor;
import com.xk.cart.pojo.Cart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CartService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static final String KEY_PREFIX="cart:ui:";
    /**
     * 新增商品
     * @param cart
     */
    public void addCart(Cart cart) {
        Integer newNum=cart.getNum();//记录传递过来的数量
        String hashKey=cart.getSkuId().toString();
        //获得登录用户
        String key=getKeyPrefixUser();
        //判断当前购物车商品是否存在
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(key);
        if (hashOps.hasKey(hashKey)){//是，修改数量
            //获购物车中数据
            String json=hashOps.get(hashKey).toString();
            //将json转换成对象，更改数量
            cart=JsonUtils.toBean(json, Cart.class);//老cart
            cart.setNum(cart.getNum()+newNum);
        }
        //将cart又转换回json存入到redis中
        hashOps.put(hashKey,JsonUtils.toString(cart));
    }

    /**
     * 查询购物车列表
     * @return
     */
    public List<Cart> queryCartList() {
        //获得登录用户
        String key=getKeyPrefixUser();
        //判断有没有购物车，没有直接返回
        if (!redisTemplate.hasKey(key)){
            return null;
        }
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(key);
        List<Object> jsonCarts=hashOps.values();
        if (CollectionUtils.isEmpty(jsonCarts)){
            return null;
        }
     /*   //遍历集合
        for (Object jsonCart:jsonCarts){
            Cart cart = JsonUtils.toBean((String) jsonCart, Cart.class);
            cartList.add(cart);
        }*/
        List<Cart> cartList = jsonCarts.stream().map(oj -> JsonUtils.toBean(oj.toString(), Cart.class)).collect(Collectors.toList());
        return cartList;
    }
    /**
     * 删除购物车中的商品
     * @param skuId
     */
    public void delCartBuySkuId(String skuId) {

        //获得登录用户
        String key=getKeyPrefixUser();
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(key);
        Long i = hashOps.delete(skuId);
        if (i==0){
           log.error("删除商品失败");
        }

    }
    /**
     * 修改购物车中商品的数量
     * @param skuId
     * @param num
     */
    public void updateCartNum(String skuId, String num) {

        //获得登录用户
        String key=getKeyPrefixUser();
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(key);
        //得到购物车
        String jsonCart = hashOps.get(skuId).toString();
        Cart cart = JsonUtils.toBean(jsonCart, Cart.class);
        cart.setNum(Integer.parseInt(num));
        hashOps.put(skuId,JsonUtils.toString(cart));

    }

    /**
     * 前缀+用户id
     * @return
     */
    private String getKeyPrefixUser(){
        UserInfo user = CartInterceptor.getUser();
        String key=KEY_PREFIX+user.getId();
        return key;
    }

}
